package com.egao.common.module.student.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egao.common.component.minio.config.MinioBucket;
import com.egao.common.component.minio.service.MinioService;
import com.egao.common.core.web.PageParam;
import com.egao.common.module.student.entity.Student;
import com.egao.common.module.student.entity.StudentBasic;
import com.egao.common.module.student.entity.vo.StudentFormVO;
import com.egao.common.module.student.mapper.StudentBasicMapper;
import com.egao.common.module.student.mapper.StudentMapper;
import com.egao.common.module.student.service.StudentBasicService;
import com.egao.common.module.system.constants.DictConstants;
import com.egao.common.module.system.entity.Dict;
import com.egao.common.module.system.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 学校基层接口实现类
 *
 * @author lsh
 */
@Service
public class StudentBasicServiceImpl extends ServiceImpl<StudentBasicMapper, StudentBasic> implements StudentBasicService {

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private DictService dictService;
    @Autowired
    private MinioService minioService;


    /**
     * 学校基层列表
     *
     * @param page
     * @return
     */
    @Override
    public List<StudentBasic> listPage(PageParam<StudentBasic> page) {
        return baseMapper.listPage(page);
    }

    /**
     * 学校基层详情
     *
     * @param student
     * @return
     */
    @Override
    public StudentBasic BasicDetails(Student student) {

        StudentFormVO studentFormVO = studentMapper.getDetail(student.getId());

        QueryWrapper<StudentBasic> qr = new QueryWrapper<>();
        qr.eq("student_id",student.getId());
        StudentBasic studentBasic = baseMapper.selectOne(qr);
        if (studentBasic == null){
            studentBasic = new StudentBasic();
            studentBasic.setStudentId(studentFormVO.getId());
        }
        studentBasic.setXh(studentFormVO.getXh());
        studentBasic.setXm(studentFormVO.getXm());
        studentBasic.setCollegeName(studentFormVO.getCollegeName());
        studentBasic.setMajorName(studentFormVO.getMajorName());
        studentBasic.setPhoneNumber(studentFormVO.getMobile());
        studentBasic.setEmail(studentFormVO.getEmail());
        studentBasic.setQqNumber(studentFormVO.getQq());
        studentBasic.setMaterialsImagePreview(minioService.getPreSignedObjectUrl(MinioBucket.MINIO_BASIC, studentBasic.getMaterialsImage()));

        List<Dict> dicts = dictService.cacheList(DictConstants.APPROVAL_STATUS);
        for (Dict d:dicts){
            if (studentBasic.getBasicServiceType() != null){
                if (studentBasic.getBasicServiceType().equals(d.getValue())){
                    studentBasic.setBasicServiceTypeName(d.getLabel());
                }
            }
        }
        studentBasic.setApprovalDictList(dicts);
        return studentBasic;
    }

    /**
     * 校基层详情
     *
     * @param id
     * @return
     */
    @Override
    public StudentBasic BasicDetailSchool(Integer id) {
        StudentBasic studentBasic = baseMapper.BasicDetailSchool(id);
        List<Dict> dicts = dictService.cacheList(DictConstants.APPROVAL_STATUS);
        studentBasic.setApprovalDictList(dicts);
        return studentBasic;
    }
}
